package com.ruoyi.asset.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.asset.domain.AssetFlowType;
import com.ruoyi.asset.domain.AssetInfo;
import com.ruoyi.asset.mapper.AssetInfoMapper;
import com.ruoyi.asset.service.IAssetFlowCheckService;
import com.ruoyi.asset.service.IAssetFlowTypeService;
import com.ruoyi.asset.service.IAssetInfoService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.AssetFlowMapper;
import com.ruoyi.asset.domain.AssetFlow;
import com.ruoyi.asset.service.IAssetFlowService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资产工单Service业务层处理
 * 
 * @author yepanpan
 * @date 2024-12-20
 */
@Service
public class AssetFlowServiceImpl implements IAssetFlowService 
{
    @Autowired
    private AssetFlowMapper assetFlowMapper;
    @Autowired
    private IAssetInfoService assetInfoService;
    @Autowired
    private IAssetFlowTypeService assetFlowTypeService;
    @Autowired
    private IAssetFlowCheckService assetFlowCheckService;


    /**
     * 查询资产工单
     *
     * @param num 资产工单编号
     * @return 资产工单
     */
    @Override
    public AssetFlow selectAssetFlowByNum(String num){
        return assetFlowMapper.selectAssetFlowByNum(num);
    }

    /**
     * 查询资产工单
     * 
     * @param id 资产工单主键
     * @return 资产工单
     */
    @Override
    public AssetFlow selectAssetFlowById(Long id)
    {
        return assetFlowMapper.selectAssetFlowById(id);
    }

    /**
     * 查询资产工单列表
     * 
     * @param assetFlow 资产工单
     * @return 资产工单
     */
    @Override
    public List<AssetFlow> selectAssetFlowList(AssetFlow assetFlow)
    {
        return assetFlowMapper.selectAssetFlowList(assetFlow);
    }

    /**
     * 新增资产工单
     * 
     * @param assetFlow 资产工单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAssetFlow(AssetFlow assetFlow)
    {
        AssetFlowType assetFlowType = assetFlowTypeService.selectAssetFlowTypeById(assetFlow.getTypeId());
        if(!assetFlowType.getCode().equalsIgnoreCase(AssetFlow.TYPE_PURCHASE)){
            AssetInfo assetInfo = assetInfoService.selectAssetInfoById(assetFlow.getAssetId());
            if(assetInfo == null || assetInfo.getIsUse().equalsIgnoreCase("Y")){
                throw new ServiceException("必须选择一个有效的闲置资产");
            }
            assetFlow.setCateId(assetInfo.getCateId());
            assetFlow.setAssetNum(assetInfo.getNum());
            assetFlow.setAmount(1);
        }
        if(StringUtils.isEmpty(assetFlow.getFlowStatus())){
            assetFlow.setFlowStatus(AssetFlow.STATUS_UN_SUBMIT);
        }
        assetFlow.setNum(assetFlowType.getCode()+ DateUtils.dateTimeNow(DateUtils.YYYYMMDDHHMMSS));
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        assetFlow.setUserId(sysUser.getUserId());
        assetFlow.setDeptId(sysUser.getDeptId());
        assetFlow.setApplyTime(new Date());
        int ret = assetFlowMapper.insertAssetFlow(assetFlow);
        if(assetFlow.getFlowStatus().equalsIgnoreCase(AssetFlow.STATUS_CHECK)){
            assetFlowCheckService.submitAssetFlowCheck(assetFlow.getId());
        }
        return ret;
    }

    /**
     * 修改资产工单
     * 
     * @param assetFlow 资产工单
     * @return 结果
     */
    @Override
    public int updateAssetFlow(AssetFlow assetFlow)
    {
        return assetFlowMapper.updateAssetFlow(assetFlow);
    }

    /**
     * 批量删除资产工单
     * 
     * @param ids 需要删除的资产工单主键
     * @return 结果
     */
    @Override
    public int deleteAssetFlowByIds(Long[] ids)
    {
        return assetFlowMapper.deleteAssetFlowByIds(ids);
    }

    /**
     * 删除资产工单信息
     * 
     * @param id 资产工单主键
     * @return 结果
     */
    @Override
    public int deleteAssetFlowById(Long id)
    {
        return assetFlowMapper.deleteAssetFlowById(id);
    }
}
